package com.paas.form.core.element.control.impl.searchItem;

import com.paas.common.util.BeanUtil;
import com.paas.common.util.StringUtil;
import com.paas.form.core.element.base.Utilmethod;
import com.paas.form.core.element.control.FormItem;
import com.paas.form.core.element.field.FieLdsModel;
import com.paas.form.core.element.jsContent.ScriptContent;
import lombok.Data;


@Data
public class PfTreeSelect implements FormItem {
    private FieLdsModel field;
    private String htmlType;

    public static final String PF_TREE_SELECT_JS_DATA = "                ${field.vModel}attrOptions : JSON.parse('${field.options}'), \n"+
            "                ${field.vModel}attrProps : JSON.parse('${field.props.props}'), ";

    public String MakeJsData(){
        String dataStr = "";
        if(BeanUtil.isNotEmpty(this.field) && StringUtil.isNotEmpty(this.field.getOptions())){
            dataStr= dataStr +"                ${field.vModel}attrOptions : JSON.parse('${field.options}'), \n";
        }else {
            dataStr= dataStr +"                ${field.vModel}attrOptions : [], \n";
        }
        if(BeanUtil.isNotEmpty(this.field) && BeanUtil.isNotEmpty(this.field.getProps())){
            dataStr= dataStr +"                ${field.vModel}attrProps : JSON.parse('${field.props.props}'),\n ";
        }else {
            dataStr= dataStr +"                ${field.vModel}attrProps : [],\n";
        }
        return dataStr;
    }


    public static final String PF_TREE_SELECT ="<pfTreeSelect v-model=\"${formObject}.${field.vModel}\" placeholder=\"请选择${field.config.label}\"\n" +
            "                :options=\"${field.vModel}attrOptions\" :props=\"${field.vModel}attrProps\" :multiple=\"${field.multiple}\" class=\"item\" clearable />";

    public PfTreeSelect(FieLdsModel field, String htmlType){
        this.field = field;
        this.htmlType = htmlType;

    }

    public String formMakeVue(){
        String elementStart = "<" +this.field.getConfig().getTag() + " v-model=\"${formObject}.${field.vModel}\"  ";
        String attrsStr = Utilmethod.attriAdd(this.field);
        String elementEnd = ":options=\"${field.vModel}attrOptions\" :props=\"${field.vModel}attrProps\"   ></" + this.field.getConfig().getTag() +">\n";
        String element= elementStart +attrsStr +elementEnd;
        return element;
    }


    @Override
    public String makeVue() {
        if(this.htmlType.equals("form")){
            return this.formMakeVue();
        }else {
            return PF_TREE_SELECT;
        }
    }

    @Override
    public ScriptContent makeJs() {
        ScriptContent scriptContent = new ScriptContent();
        scriptContent.setData(this.MakeJsData());
        return scriptContent;
    }
}
